package user

import (
	"fmt"
	"gitee.com/binny_w/gin_starter/core"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func afterInsert(c *gin.Context) {
	c.Next()
	id := c.GetInt64("new_id")
	role := c.GetString("role")
	if id != 0 && role != "" {
		ok, err := core.Rbac.AddRolesForUser(fmt.Sprintf("user:%d", id), role)
		if !ok || err != nil {
			core.Logger.Error("fail to add role to user", zap.String("role", role), zap.Int64("newId", id), zap.Bool("ok", ok), zap.Error(err))
		}
	}
}
